﻿namespace Admin.NET.Application.Service;

/// <summary>
/// 血液库存
/// </summary>
public class InvKuCunService : BaseService<InvKuCun>
{

    public InvKuCunService(IServiceScopeFactory scopeFactory) : base(scopeFactory)
    {
    }

    /// <summary>
    /// 获取列表
    /// </summary>
    public async Task<SqlSugarPagedList<InvKuCunOutput>> Page(InvKuCunPageInput input)
    {
        return await Queryable
            .WhereIF(input.xianXueMa.HasValue(), it => it.xianXueMa == input.xianXueMa)
            .WhereIF(input.rongLiang > 0, it => it.rongLiang == input.rongLiang)
            .WhereIF(input.shiXiaoStartTime is not null, it => it.shiXiaoShiJian >= input.shiXiaoStartTime)
            .WhereIF(input.shiXiaoEndTime is not null, it => it.shiXiaoShiJian <= input.shiXiaoEndTime)
            .WhereIF(input.ruKuStartTime is not null, it => it.CreateTime >= input.ruKuStartTime)
            .WhereIF(input.ruKuEndTime is not null, it => it.CreateTime >= input.ruKuEndTime)
            .WhereIFList(input.listYuanQuId, "yuanQuId")
            .WhereIFList(input.listKuCunZhuangTai, "kuCunZhuangTai")
            .WhereIFList(input.listABO, "abo")
            .WhereIFList(input.listRhD, "rhD")
            .WhereIFList(input.listXueYePinZhong, "pinZhongId")
            .Select<InvKuCunOutput>()
            .ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 通过Id获取
    /// </summary>
    public InvKuCunOutput Get(long id)
    {
        return GetById<InvKuCunOutput>(id);
    }

    /// <summary>
    /// 扫码获取血液
    /// </summary>
    public List<InvKuCunOutput> GetByCode(string code)
    {
        // code==献血码  或者 code前部分是献血码,后部分是产品码
        var bloods = Queryable
            .Where(inv => inv.xianXueMa == code || ((inv.xianXueMa + inv.chanPinId).StartsWith(code) && code.StartsWith(inv.xianXueMa)))
            .Select<InvKuCunOutput>()
            .ToList();
        return bloods;
    }

    /// <summary>
    /// 新增成功后返回主键
    /// </summary>
    public long Add(InvKuCunInput input)
    {
        return AddWithInput(input);
    }

    /// <summary>
    /// 不能全量更新,只能部分更新
    /// </summary>
    public bool Update(InvKuCunUpdateInput input)
    {
        //方法1
        return _rep.AsUpdateable(input.Adapt<InvKuCun>())
                .UpdateColumns(it => new
                {
                    it.yuanQuId,
                    it.jiaGe,
                    it.kuCunZhuangTai,
                    it.teShuChuLi,
                    it.huiShouZhuangTai,
                    it.laiYuanId,
                    it.xueDaiWaiGuan,
                    it.ziBianHao,
                    it.fuHeABO,
                    it.fuHeRhD,
                    it.rhFenXing,
                    it.ruKuShiJian,
                    it.remark,
                })
                .ExecuteCommand() > 0;
    }

    /// <summary>
    /// 软删除
    /// </summary>
    public bool Delete(long id)
    {
        return _rep.FakeDelete(new InvKuCun() { Id = id }) > 0;
    }

    /// <summary>
    /// 检查血液是否已存在库存中
    /// </summary>
    public bool CheckXueYeShiFouCunZai(InvKuCunCheckCunZaiInput input)
    {
        return Queryable
            .Where(t => t.xianXueMa == input.xianXueMa && t.chanPinId == input.chanPinId)
            .Count() > 0;
    }
}
